package ah.file;

import java.io.File;

/**
 * 模仿cmd命令中的tree打印
 * @author ddh
 *
 */
public class CMDTree {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//├─　──　│　└─　
		if (args.length==0) {
			args = new String[]{".."};
		}
		String fileStr = "C:\\Documents and Settings";
		File file = new File(fileStr);
		if (file.isFile()) {
			System.out.println(file.getName());
		} else {
			new CMDTree().print("", new File(fileStr));
		}
	}
	/**
	 * 
	 * @param prefix
	 * @param file
	 */
	public void print(String prefix, File file) {
		//先判断是否是目录
		File[] files = file.listFiles(new HiddenFileFilter());
		int length = files.length;
		prefix = prefix.replace("─", " ").replace("├", "│").replace("└", "│");
		if (length==0) {
			return;
		} else if (length==1) {
			if (files[0].isDirectory()) {
				System.out.println(prefix + " └─" + files[0].getName());
				print(prefix + "   ", files[0]);
			}
		} else if (length>1) {
			for (int i=0;i<length-1;i++) {
				if (files[i].isDirectory()) {
					System.out.println(prefix + " ├─" + files[i].getName());
					print(prefix + " ├─", files[i]);
				}
			}
			if (files[length-1].isDirectory()) {
				System.out.println(prefix + " └─" + files[length-1].getName());
				print(prefix + "   ", files[length-1]);
			}
		}
		
	}
}
